package gold.digger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC517 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 不是因为复杂，而是因为我要赶火车～～
     * 未来需要再次复习此道题目
     * @return:
     */
    class Solution {
        public int findMinMoves(int[] machines) {
            int tot = Arrays.stream(machines).sum();
            int n = machines.length;
            if (tot % n != 0) {
                return -1;
            }
            int avg = tot / n;
            int ans = 0, sum = 0;
            for (int num : machines) {
                num -= avg;
                sum += num;
                ans = Math.max(ans, Math.max(Math.abs(sum), num));
            }
            return ans;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC517 an = new LC517();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
